import { createApp } from "vue";
import App from "./App.vue";
import { createPinia } from "pinia";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";
import setComponents from "./components";
import i18n from "./i18n";
import uviewPlus from "uview-plus";
import "uview-plus/index.scss";
import { useStore } from "@/stores";
import {
	sendMessage
} from "@/util/websocket";
	

// #ifdef H5
import vConsole from "vconsole";
new vConsole();
// #endif

import {
	formattingTime,
	shouldShowTime,
	goTo,
	goToTab,
	goBack,
	goReLaunch,
	goRedirectTo,
	copyText,
	deepClone,
	getImageDimensions,
	typeCopywriting,
	setChatList,
	getLastFourChars,
	getChatList,
	getChatListNumber,
	deletingData,
	getUnreadCount,
	handleCall,
	playVoice,
	pauseVoice,
	stopVoice,
} from "@/static/ts/common";
import {
	PersonalSend
} from "@/api/ImApi";
import { getRsaCode } from "@/static/ts/rsa";

// #ifdef H5
import IndexedDB from "../db/indexedDB";
// #endif
// #ifdef APP-PLUS
import SQLite from "../db/sqlite";
// #endif

// 创建 pinia 实例
const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);

// 数据库初始化函数
async function initializeDatabase() {
	let db : any;
	try {
		// #ifdef APP-PLUS
		db = new SQLite("myChatApp");
		await db.init();
		console.log("SQLite 数据库初始化成功");
		await db.createTables();
		console.log("SQLite 表创建成功");
		// #endif

		// #ifdef H5
		db = new IndexedDB("myChatApp");
		await db.init();
		console.log("IndexedDB 数据库初始化成功");
		// #endif

		// 返回数据库实例
		return db;
	} catch (error) {
		console.error("数据库初始化失败:", error);
		return null;
	}
}

// 启动应用的方法
async function startApp() {
	const db = await initializeDatabase();
	if (db) {
		const app = createApp(App);

		// 使用插件和全局方法
		app.use(pinia);
		app.use(i18n);
		app.use(uviewPlus);

		app.config.globalProperties.formattingTime = formattingTime;
		app.config.globalProperties.shouldShowTime = shouldShowTime;
		app.config.globalProperties.getRsaCode = getRsaCode;

		// 跳转方法注入
		app.config.globalProperties.goTo = goTo;
		app.config.globalProperties.goToTab = goToTab;
		app.config.globalProperties.goBack = goBack;
		app.config.globalProperties.goReLaunch = goReLaunch;
		app.config.globalProperties.goRedirectTo = goRedirectTo;
		app.config.globalProperties.copyText = copyText;
		app.config.globalProperties.deepClone = deepClone;
		app.config.globalProperties.getImageDimensions = getImageDimensions;
		app.config.globalProperties.typeCopywriting = typeCopywriting;
		app.config.globalProperties.setChatList = setChatList;
		app.config.globalProperties.getLastFourChars = getLastFourChars;
		app.config.globalProperties.getChatList = getChatList;
		app.config.globalProperties.getChatListNumber = getChatListNumber;
		app.config.globalProperties.deletingData = deletingData;
		app.config.globalProperties.getUnreadCount = getUnreadCount;
		app.config.globalProperties.handleCall = handleCall;
		app.config.globalProperties.playVoice = playVoice;
		app.config.globalProperties.pauseVoice = pauseVoice;
		app.config.globalProperties.stopVoice = stopVoice;

		// 注册组件
		setComponents(app);

		// 注入数据库实例到全局属性
		app.config.globalProperties.$db = db;

		// 设置数据库到 Pinia store
		const store = useStore();
		store.setDb(db);

		// 挂载应用
		app.mount("#app");
		const globalData = getApp().globalData || {};
		globalData.sendMessage = sendMessage;
		globalData.goBack = goBack;
		globalData.store = store;
		globalData.$db = db;
		globalData.PersonalSend = PersonalSend;
		globalData.setChatList = setChatList;
		globalData.playVoice = playVoice;
		globalData.pauseVoice = pauseVoice;
		globalData.stopVoice = stopVoice;
		
		getApp().globalData = globalData;

	} else {
		console.error("数据库初始化失败，无法启动应用！");
	}
}

// 初始化并启动应用
startApp();